/**
 * 初始化routes详情对话框
 */
var RoutesInfoDlg = {
    routesInfoData : {},
    pNameZtree: null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '名称不能为空'
                }
            }
        },
        component: {
            validators: {
                notEmpty: {
                    message: '组件不能为空'
                }
            }
        } ,
         code: {
            validators: {
                notEmpty: {
                    message: 'code不能为空'
                }
            }
        } ,
         num: {
            validators: {
                notEmpty: {
                    message: '排序不能为空'
                }
            }
        }
        
    }
};

/**
 * 清除数据
 */
RoutesInfoDlg.clearData = function() {
    this.routesInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
RoutesInfoDlg.set = function(key, val) {
    this.routesInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
RoutesInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
RoutesInfoDlg.close = function() {
    parent.layer.close(window.parent.Routes.layerIndex);
}

/**
 * 显示父级菜单的树
 *
 * @returns
 */
RoutesInfoDlg.showPNameSelectTree = function () {
    Feng.showInputTree("pName", "pNameContent");
};

/**
 * 收集数据
 */
RoutesInfoDlg.collectData = function() {
    this.set('id').set('name').set('pid').set('component').set('path').set('iconCls').set('leaf').set('hidden').set('kye').set('code').set('num');
}

/**
 * 提交添加
 */
RoutesInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/routes/add", function(data){
        Feng.success("添加成功!");
        window.parent.Routes.table.refresh();
        RoutesInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.routesInfoData);
    ajax.start();
}
/**
 * 点击父级菜单input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
RoutesInfoDlg.onClickPName = function (e, treeId, treeNode) {
    $("#pName").attr("value", RoutesInfoDlg.pNameZtree.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
};

/**
 * 提交修改
 */
RoutesInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/routes/update", function(data){
        Feng.success("修改成功!");
        window.parent.Routes.table.refresh();
        RoutesInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.routesInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("routesInfoForm", RoutesInfoDlg.validateFields);
    var pNameTree = new $ZTree("pNameTree", "/routes/routesTreeList");
    pNameTree.bindOnClick(RoutesInfoDlg.onClickPName);
    pNameTree.init();
    RoutesInfoDlg.pNameZtree = pNameTree;
});
